# -*- coding:utf-8 -*-

from fake_useragent import UserAgent
from lxml import etree
import time
from datetime import datetime
import base64
import requests
import hashlib

from requests.auth import HTTPProxyAuth


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
使用代理
讯代理-动态转发
http://www.xdaili.cn/
"""



# 代理服务器
proxyIp = "forward.xdaili.cn"
proxyPort = "80"
# 代理隧道验证信息
orderno = "ZF20179xxxxxxxxx"
secret = "d70615e169054cc6bbb532e684e74949"

ip_port = proxyIp + ":" + proxyPort

def get_sign():
    # 获取签名
    timestamp = str(int(time.time()))
    string = ""
    string = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp
    string = string.encode()
    md5_string = hashlib.md5(string).hexdigest()
    sign = md5_string.upper()
    auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp
    return auth


def get_request_proxy(method, url, headers_s, data, retry=1):
    # 获取签名
    auth = get_sign()
    ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
    headers_p = {
        "User-Agent": ua.random,
        "Proxy-Authorization": auth,
    }
    # 合并头
    headers = dict(headers_s, **headers_p)

    proxies = {
        "http": "http://" + ip_port,
        "https": "https://" + ip_port
    }
    res = None
    try:
        if method == "POST":
            if url.find("https") == 0:
                res = requests.post(url, headers=headers, data=data, proxies=proxies, verify=False)
            else:
                res = requests.post(url, headers=headers, data=data, proxies=proxies)
        elif method == "GET":
            if url.find("https") == 0:
                res = requests.get(url, headers=headers, params=data, proxies=proxies, verify=False)
            else:
                res = requests.get(url, headers=headers, params=data, proxies=proxies)
        else:
            res = None
    except Exception as ex:
        print(f"-------------【错误】,重试第【{retry}】次-------------")
        print(ex)
        retry += 1
        return get_request_proxy(method, url, headers, data, retry)
    else:
        if res.status_code != 200:
            max_retry = 3  # 重试次数
            print(f"-------------返回状态码:{res.status_code},重试第【{retry}】次-------------")
            retry += 1
            return get_request_proxy(method, url, headers, data, retry)
    return res


if __name__ == '__main__':
    url = "https://ip.cn/"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',

    }
    for i in range(5):
        start = datetime.now()
        html = get_request_proxy("GET", url, headers, "")
        end = datetime.now()
        root = etree.HTML(html.text)
        print("".join(root.xpath('//div[@id="result"]//text()')))
        print(f"====耗时：{str(end - start)}====")